package com.hutu.service.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hutu.mapper.biz.PublicVarsMapper;
import com.hutu.model.biz.PublicVars;
import com.hutu.model.resp.Response;
import com.hutu.model.resp.ResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hutu.service.biz.PublicVarsService;

/**
 * <p>
 * 模板 服务类
 * </p>
 *
 * @author admin
 * @since 2023-07-20
 */
@Service
public class  PublicVarsServiceImpl extends ServiceImpl<PublicVarsMapper,PublicVars > implements PublicVarsService{

    public ResponseVo add(PublicVars publicVars){
        if(save(publicVars)){
            return Response.success();
        }
        return Response.error();
    }

    public ResponseVo deleteById(Integer id){
        if(removeById(id)){
            return Response.success();
        }
        return Response.error();
    }

    public ResponseVo deleteBatch(List<Integer> ids){
        if(removeBatchByIds(ids)){
            return Response.success();
        }
        return Response.error();
    }


    public ResponseVo edit(PublicVars publicVars){
        if(updateById(publicVars)){
            return Response.success();
        }
        return Response.error();
    }


    public ResponseVo findById(Integer id){
        PublicVars publicVars =getById(id);
        if(publicVars !=null){
            return Response.success(publicVars);
        }
        return Response.error();
    }


    public ResponseVo list(Integer current,Integer size,String name){
        IPage<PublicVars> config=new Page<>(current,size);
        QueryWrapper<PublicVars> wrapper=null;
        if(name!=""&&name!=null){
            wrapper=new QueryWrapper<>();
            wrapper.like("name",name);
        }
        IPage<PublicVars> page=page(config,wrapper);
        // 将响应数据填充到map中
        Map map=new HashMap();
        map.put("pages",page.getPages());
        map.put("total",page.getTotal());
        map.put("list",page.getRecords());
        return Response.success(map);
    }

    @Override
    public List<PublicVars> listAll(String project) {
        LambdaQueryWrapper<PublicVars> lambdaQueryWrapper = new LambdaQueryWrapper<PublicVars>().eq(PublicVars::getProject, project);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
    * 数据导出
    *
    * @param response
    * @return
    */
    public ResponseVo export(HttpServletResponse response)throws IOException{
        List<PublicVars> list=list();
        // 通过工具类创建对象，可以指定磁盘路径
        ExcelWriter writer=ExcelUtil.getWriter(true);
        // 一次性将list内的数据写入到excel，使用默认样式,强制输出标题
        writer.write(list,true);
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String filename=URLEncoder.encode("数据","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
        ServletOutputStream outputStream=response.getOutputStream();
        writer.flush(outputStream,true);
        outputStream.close();
        writer.close();
        return Response.success();
    }

    /**
     * 数据导入
     *
     * @param file
     * @return
     */
    public ResponseVo imp(MultipartFile file)throws IOException{
        InputStream inputStream=file.getInputStream();
        ExcelReader reader=ExcelUtil.getReader(inputStream);
        List<PublicVars> list=reader.readAll(PublicVars.class);
        // IService接口中的方法.批量插入数据
        if(saveBatch(list)){
            return Response.success();
        }
        return Response.error();
    }


}




